﻿using DomainService.IService;
using Microsoft.AspNetCore.Mvc;
using Utility.Enum;
using Model.DomainModel;
using Utility;
using Model.BusinessModel.OutPut;
using Model.BusinessModel;
using Model.BusinessModel.InPut;

namespace ApprovalFlow_WebApi.Controllers
{
    /// <summary>
    /// 流程模块
    /// </summary>
    [ApiController]
    [Route("api/[controller]/[action]")]
    [ApiExplorerSettings(GroupName = nameof(ModeuleGroupEnum.Flow))]
    public class FlowController : BaseController
    {
        #region 构造实列化

        /// <summary>
        /// 表单服务
        /// </summary>
        public IFormService _formService;

        /// <summary>
        /// 流程服务
        /// </summary>
        public IFlowService _flowService;


        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="formService">表单服务</param>
        /// <param name="flowService">流程服务</param>
        public FlowController(IFormService formService, IFlowService flowService)
        {
            _formService = formService;
            _flowService = flowService;
        }

        #endregion

        #region 接口

        /// <summary>
        /// 新增流程
        /// </summary>
        /// <param name="flow">实体模型</param>
        /// <returns></returns>
        [HttpPost]
        public ReceiveStatus Insert(FlowInput flow)
        {
            flow.flow.CreateTime = DateTime.Now;
            flow.flow.CreateUserKey = GetLoginUserMsg().UserId.ToString();
            return _flowService.Insert(flow);
        }

        /// <summary>
        /// 更新流程信息
        /// </summary>
        /// <param name="flow">实体模型</param>
        /// <returns></returns>
        [HttpPost]
        public ReceiveStatus Update(FlowInput flow)
        {
            flow.flow.CreateTime = DateTime.Now;
            flow.flow.CreateUserKey = GetLoginUserMsg().UserId.ToString();
            return _flowService.Update(flow);
        }

        /// <summary>
        /// 获取所有流程
        /// </summary>
        /// <param name="pageResultModel">分页参数模型</param>
        /// <returns></returns>
        [HttpPost]
        public ReceiveStatus<List<FlowOutPut>> GetAllFlow(PageResultModel pageResultModel)
        {
            ReceiveStatus<List<FlowOutPut>> receiveStatus = new();
            var result = _flowService.GetAllFlow(pageResultModel);
            receiveStatus.data = result.data;
            receiveStatus.total = result.total;
            return receiveStatus;
        }

        /// <summary>
        /// 根据主键获取流程
        /// </summary>
        /// <param name="flowId">流程主键</param>
        /// <returns></returns>
        [HttpGet]
        public ReceiveStatus<Flow> GetFlowByFlowId(string flowId)
        {
            return _flowService.GetFlowByFlowId(flowId);
        }

        /// <summary>
        /// 获取流程节点配置
        /// </summary>
        /// <param name="flowId">流程id</param>
        /// <param name="flowNodeId">节点id</param>
        /// <returns></returns>
        [HttpGet]
        public ReceiveStatus<FlowNodeConfigOutPut> GetExpireDate(string flowId, string flowNodeId)
        {
            return _flowService.GetExpireDate(flowId, flowNodeId);
        }

        /// <summary>
        /// 更新数据状态
        /// </summary>
        /// <param name="input">输入模型</param>
        /// <returns></returns>
        [HttpPost]
        public ReceiveStatus UpdateFlowDataState(UpdateDataStateInput input)
        {
            return _flowService.UpdateFlowDataState(input);
        }

        /// <summary>
        /// 获取部门流程树形结构
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ReceiveStatus<List<TreeOutPut>> GetDepartmentFlow()
        {
            var loginModel = GetLoginUserMsg();
            return _flowService.GetDepartmentFlow(loginModel.CorporationKey);
        }

        /// <summary>
        /// 根据流程id获取流程和表单数据
        /// </summary>
        /// <param name="flowId">流程id</param>
        /// <returns></returns>
        [HttpGet]
        public ReceiveStatus<FlowOrFormOutPut> GetFlowOrFormByFlowId(string flowId)
        {
            return _flowService.GetFlowOrFormByFlowId(flowId);
        }
        #endregion
    }
}
